/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cci;

import cdp.Cliente;
import cgt.AplCadastrarCliente;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Rodrigo
 */
public class CtrlCliente extends HttpServlet {
    
    String janela;
    String opcao = "novo";

    /*Atributos Cliente*/
    Cliente cliente = new Cliente();
    long id; //id do item atual
    String nome;
    int idade;
    String endereco;
    String senha;
    
    AplCadastrarCliente apl = AplCadastrarCliente.getInstance();
    
    private void setParametros(HttpServletRequest request, HttpServletResponse response)
    {
        janela = request.getParameter("janela");
        opcao = request.getParameter("opcao");
    }
    
    private void setParametrosCliente(HttpServletRequest request, HttpServletResponse response)
    {
        if (janela.equals("Menu")) {   
            cliente = (Cliente) request.getSession().getAttribute("cliente");
            id = cliente.getId();
            nome = cliente.getNome();
            idade = cliente.getIdade();
            endereco = cliente.getEndereco();
            senha = cliente.getSenha();
            
        }
        
    }
    
    private void setAtributosCliente(HttpServletRequest request, HttpServletResponse response){
        request.setAttribute("id", id);
        request.setAttribute("nome", nome);
        request.setAttribute("idade", idade);
        request.setAttribute("endereco", endereco);
        request.setAttribute("senha", senha);
    }
    
    private void salvarCliente(HttpServletRequest request, HttpServletResponse response){
        List<Object> lista = new ArrayList<Object>();
        
        nome = request.getParameter("nome");
        idade = Integer.parseInt(request.getParameter("idade"));
        endereco = request.getParameter("endereco");
        senha = request.getParameter("senha");
        
        lista.add(nome);
        lista.add(idade);
        lista.add(endereco);
        lista.add(senha);
        
        if (opcao.equals("novo")) {
            apl.incluirCliente(lista);
        }
        else{
            lista.add(id);
            apl.alterarCliente(lista);
        }
    }
    
    private void excluirCliente(HttpServletRequest request, HttpServletResponse response){
        cliente = apl.consultar(cliente, id);
        apl.excluir(cliente);
    }
    
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet CtrlCliente</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet CtrlCliente at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
             */
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        String url = "/";
        RequestDispatcher dispatcher = null;
        
        setParametros(request, response);
        
        //TODO ver de onde vem as chamadas!
        if(janela.equals("Menu")){
            
            url = "cih/PagFormCliente.jsp";
            
            if(opcao.equals("alterar")){ 
                setParametrosCliente(request, response);
                setAtributosCliente(request, response);
                dispatcher = request.getRequestDispatcher(url);
                dispatcher.forward(request, response);
            }
            
//            if(opcao.equals("excluir")){
//                setParametrosCliente(request, response);
//                excluirCliente(request, response);
//                response.sendRedirect("cih/PagEventosCliente.jsp");
//            }
        }
        
//        if(janela.equals("EventosCliente")){
//            
//            url = "cih/PagFormCliente.jsp";
//            
//            if(opcao.equals("novo")){
//              dispatcher = request.getRequestDispatcher(url);
//              dispatcher.forward(request, response);
//            }
//            if(opcao.equals("alterar")){ 
//                setParametrosCliente(request, response);
//                setAtributosCliente(request, response);
//                dispatcher = request.getRequestDispatcher(url);
//                dispatcher.forward(request, response);
//            }
//            if(opcao.equals("excluir")){
//                setParametrosCliente(request, response);
//                excluirCliente(request, response);
//                response.sendRedirect("cih/PagEventosCliente.jsp");
//            }
//        }
        
        if(janela.equals("FormCliente")){   
            salvarCliente(request, response); 
            
            if (opcao.equals("novo")) {
                response.sendRedirect("Login.jsp");
            }
            else{
                response.sendRedirect("index.jsp");
            }
            
        }
        
        //processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
